package cn.message.service.impl;

import cn.itcast.mapper.districtMapper;
import cn.itcast.pojo.district;
import cn.itcast.pojo.districtExample;
import cn.message.service.IDistrictService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-05-11 9:20
 */
@Service
public class DistrictServiceImpl implements IDistrictService {

    @Resource
    private cn.itcast.mapper.districtMapper districtMapper;


    @Override
    public Map<String, Object> getAllDistrict(Map<String, String> map) {
        int page = Integer.parseInt(map.get("page"));
        int limit = Integer.parseInt(map.get("limit"));
        districtExample districtExample = new districtExample();
        int count = districtMapper.countByExample(districtExample);
        PageHelper.startPage(page,limit);
        List<district> districtList = districtMapper.selectByExample(districtExample);

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count",count);
        resultMap.put("districtList",districtList);
        return resultMap;
    }


    @Override
    public Map<String, Object> getAllProvince(Map<String, String> map) {
        districtExample districtExample = new districtExample();
        cn.itcast.pojo.districtExample.Criteria criteria = districtExample.createCriteria();
        criteria.andTypeEqualTo(1);
        List<district> provinceList = districtMapper.selectByExample(districtExample);

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("provinceList",provinceList);
        return resultMap;
    }

    @Override
    public Map<String, Object> getChildrenDistrict(Map<String, String> map) {
        int id = Integer.parseInt(map.get("id"));

        //根据pid获取子区域信息
        List<district> districtList = districtMapper.getChildrenDistrict(id);

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("districtList",districtList);
        return resultMap;
    }

    @Override
    public Map<String, Object> findDistrict(Map<String, String> map) {
        int page = Integer.parseInt(map.get("page"));
        int limit = Integer.parseInt(map.get("limit"));
        districtExample districtExample = new districtExample();
        cn.itcast.pojo.districtExample.Criteria criteria = districtExample.createCriteria();
        if(!map.get("id").isEmpty()){
            //根据id查询该区域的区域类型，如果type等于3，则是区
            Integer type = districtMapper.getDistrictById(Integer.parseInt(map.get("id"))).getType();
            if(type == 3){
                criteria.andIdEqualTo(Integer.parseInt(map.get("id")));
            }else {
                criteria.andPidEqualTo(Integer.parseInt(map.get("id")));
            }
        }
        int count = districtMapper.countByExample(districtExample);
        PageHelper.startPage(page,limit);
        List<district> districtList = districtMapper.selectByExample(districtExample);

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count",count);
        resultMap.put("districtList",districtList);
        return resultMap;
    }

    @Override
    public Boolean openDistrict(String districtSqe) {
        districtExample districtExample = new districtExample();
        cn.itcast.pojo.districtExample.Criteria criteria = districtExample.createCriteria();


        criteria.andDistrictSqeLike(districtSqe+"%");

        int count = districtMapper.countByExample(districtExample);

        int a = districtMapper.openDistrict(districtSqe);

        if(a == count){
            return true;
        }
        return false;
    }

    @Override
    public Boolean closeDistrict(String districtSqe) {
        districtExample districtExample = new districtExample();
        cn.itcast.pojo.districtExample.Criteria criteria = districtExample.createCriteria();


        criteria.andDistrictSqeLike(districtSqe+"%");

        int count = districtMapper.countByExample(districtExample);

        int a = districtMapper.closeDistrict(districtSqe);

        if(a == count){
            return true;
        }
        return false;
    }
}
